﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Text;
using System.IO;
using System.Web.UI;
using System.ComponentModel;

namespace AsyncWebPart
{
    [System.Web.Script.Services.ScriptService]
    [WebService(Namespace = "http://tempuri.org/", Name = "AsyncWebPartService")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class AsyncWebPartService : System.Web.Services.WebService
    {
        /// <summary>
        /// Get the HTML for the control
        /// </summary>
        /// <param name="rowLimit"></param>
        /// <param name="usePaging"></param>
        /// <param name="cacheResultMinutes"></param>
        /// <param name="requestedStartRow"></param>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        [ScriptMethod]
        [WebMethod]
        public string GetUIHtml(String rowLimit, Boolean usePaging, int cacheResultMinutes, String requestedStartRow, String cacheKey)
        {
            AsyncWebPartUI asyncWebPartUI = new AsyncWebPartUI(
                                                            rowLimit,
                                                            usePaging, 
                                                            cacheResultMinutes, 
                                                            requestedStartRow,
                                                            cacheKey);

            StringBuilder retOutputBuilder = new StringBuilder();

            if (asyncWebPartUI != null)
            {
                using (TextWriter stringWriter = new StringWriter(retOutputBuilder))
                {
                    using (HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter))
                    {
                        asyncWebPartUI.RenderControl(textWriter);
                    }
                }
            }
            return retOutputBuilder.ToString();
        }
    }
}
